package com.jianbing.util;

import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;

/**
 * Created by Han, Tixiang on 2016/8/19 0019.
 */
public class PropertiesUtils {
    private static PropertiesConfiguration properties;

    private static PropertiesConfiguration load() {
        String fileName = System.getProperty("config");
        if (StringUtils.isEmpty(fileName)) {
            fileName = "./conf/config.properties";
            fileName = "c:\\kaifa.properties";
        }
        try {
            properties = new PropertiesConfiguration(fileName);
            //自动重新加载
            properties.setReloadingStrategy(new FileChangedReloadingStrategy());
            //自动保存
            properties.setAutoSave(true);
        } catch (Exception e) {
        }
        return properties;
    }

    public static String getProperties(String key) {
        return getProperties(key, null);
    }

    public static String getProperties(String key, String defaulValue) {
        if (properties == null) {
            synchronized (PropertiesUtils.class) {
                if (properties == null) {
                    properties = load();
                }
            }
        }

        String value = properties.getString(key);
        if (StringUtils.isEmpty(value)) {
            return defaulValue;
        }
        return value;
    }

    public static void main(String[] args) throws InterruptedException {
    }
}
